package cn.smile.business.basic.impl;

import cn.smile.bean.dto.basic.DicInfoDTO;
import cn.smile.bean.dto.basic.PickerDTO;
import cn.smile.bean.entity.basic.DicInfo;
import cn.smile.bean.form.basic.dic.DicAddForm;
import cn.smile.business.basic.IDicInfoService;
import cn.smile.commons.constant.NumberConstant;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.repository.basic.DicInfoMapper;
import cn.smile.utils.BeanCopy;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class DicInfoServiceImpl extends BaseServiceImpl<DicInfoMapper, DicInfo> implements IDicInfoService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addDic(DicAddForm form) {
        log.info("[DicInfoServiceImpl].[addDic] ------> 新增字典项 Start, form = {}", JSON.toJSONString(form));
        DicInfo entity = BeanCopy.copyBean(form, DicInfo.class);
        this.setDefaultInfo(entity);
        super.addBaseCol(entity);
        int insertNum = baseMapper.insert(entity);
        log.info("[DicInfoServiceImpl].[addDic] ------> 新增字典项 End, insertNum = {}", insertNum);
        return insertNum > NumberConstant.ZERO;
    }

    @Override
    public List<DicInfoDTO> getDicList(String parentId) {
        log.info("[DicInfoServiceImpl].[getDicList] ------> 查询字典项 Start, parentId = {}", parentId);
        if (ObjectUtils.isEmpty(parentId)) {
            parentId = NumberConstant.MINUS_ONE_STRING;
        }
        List<DicInfo> dicList = baseMapper.selectList(new QueryWrapper<DicInfo>().lambda().eq(DicInfo::getParentId, parentId).eq(DicInfo::getDeleted, Boolean.FALSE).eq(DicInfo::getIsShow, Boolean.TRUE).orderByAsc(DicInfo::getDicSort));
        List<DicInfoDTO> dataList;
        if (CollectionUtils.isEmpty(dicList)) {
            dataList = Lists.newArrayList();
        } else {
            dataList = BeanCopy.copyList(dicList, DicInfoDTO.class);
        }
        log.info("[DicInfoServiceImpl].[getDicList] ------> 查询字典项 End, dataList.size() = {}", dataList.size());
        return dataList;
    }

    @Override
    public Map<Integer, DicInfoDTO> getDicMap(String parentId) {
        log.info("[DicInfoServiceImpl].[getDicMap] ------> 查询字典项, 返回Map Start, parentId = {}", parentId);
        Map<Integer, DicInfoDTO> dataMap = Maps.newHashMap();
        List<DicInfoDTO> dicList = this.getDicList(parentId);
        if (!CollectionUtils.isEmpty(dicList)) {
            for (DicInfoDTO entity : dicList) {
                dataMap.put(entity.getDicValue(), entity);
            }
        }
        log.info("[DicInfoServiceImpl].[getDicMap] ------> 查询字典项, 返回Map End");
        return dataMap;
    }

    @Override
    public List<PickerDTO> miniProjectDicList(String parentId) {
        log.info("[DicInfoServiceImpl].[miniProjectDicList] ------> 查询字典项, 返回微信小程序对象 Start, parentId = {}", parentId);
        List<PickerDTO> dataList = Lists.newArrayList();
        List<DicInfoDTO> dicList = this.getDicList(parentId);
        if (!CollectionUtils.isEmpty(dicList)) {
            for (DicInfoDTO entity : dicList) {
                PickerDTO dto = new PickerDTO();
                dto.setLabel(entity.getDicName());
                dto.setValue(String.valueOf(entity.getDicValue()));
                dataList.add(dto);
            }
        }
        log.info("[DicInfoServiceImpl].[miniProjectDicList] ------> 查询字典项, 返回微信小程序对象 End");
        return dataList;
    }

    /**
     * 写入默认值
     *
     * @param entity 新增对象
     */
    private void setDefaultInfo(DicInfo entity) {
        List<DicInfo> dicInfoList = baseMapper.selectList(new QueryWrapper<DicInfo>().lambda().eq(DicInfo::getParentId, entity.getParentId()).eq(DicInfo::getDeleted, Boolean.FALSE).orderByDesc(DicInfo::getDicValue));
        if (!CollectionUtils.isEmpty(dicInfoList)) {
            //存在同级字典项
            int lastValue = dicInfoList.get(NumberConstant.ZERO).getDicValue();
            if (NumberConstant.MINUS_ONE_STRING.equals(entity.getParentId())) {
                lastValue += NumberConstant.ONE_THOUSAND;
            } else {
                lastValue += NumberConstant.ONE;
            }
            entity.setDicValue(lastValue);
            //默认排最后一个
            if (ObjectUtils.isEmpty(entity.getDicSort())) {
                entity.setDicSort(dicInfoList.size() + NumberConstant.ONE);
            }
        } else {
            //不存在同级字典项
            if (NumberConstant.MINUS_ONE_STRING.equals(entity.getParentId())) {
                entity.setDicValue(NumberConstant.ONE_THOUSAND);
            } else {
                DicInfo dic = baseMapper.selectOne(new QueryWrapper<DicInfo>().lambda().eq(DicInfo::getId, entity.getParentId()).eq(DicInfo::getDeleted, Boolean.FALSE));
                entity.setDicValue(dic.getDicValue() + NumberConstant.ONE);
            }
            entity.setDicSort(NumberConstant.ONE);
        }
    }
}
